import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'constants.dart';

enum ActionItems { GROUP_CHAT, ADD_FRIEND, QS_SCAN, PAYMENT, HELP }

class NavigationIconView {
  final String _title;
  final IconData _icon;
  final IconData _activeIcon;

  final BottomNavigationBarItem item;

  NavigationIconView(
      {Key? key,
      required String title,
      required IconData icon,
      required IconData activeIcon})
      : _title = title,
        _icon = icon,
        _activeIcon = activeIcon,
        item = BottomNavigationBarItem(
            icon: Icon(
              icon,
            ),
            activeIcon: Icon(
              activeIcon,
            ),
            label: title,
            backgroundColor: Color(AppColors.TabIconActive));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  late List<NavigationIconView> _navigationIconViews;
  int _currentIndex = 0;
  @override
  void initState() {
    super.initState();
    // 底部栏
    _navigationIconViews = [
      NavigationIconView(
          title: "微信",
          icon: IconData(0xe7e5, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe7e5, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: "通讯录",
          icon: IconData(0xe761, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe761, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: "发现",
          // &#xe636; ==》 0xe636
          icon: IconData(0xe636, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe636, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: "我",
          icon: IconData(0xe6c1, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe6c1, fontFamily: Constants.IconFontFamily)),
    ];
  }

  _buildPopupMenuItem(int icon, String title) {
    return Row(
      children: [
        Icon(IconData(icon, fontFamily: Constants.IconFontFamily),
            color: Color(AppColors.AppBarPopupMenuTextColor)),
        Container(
          width: 12,
        ),
        Text(title,
            style: TextStyle(color: Color(AppColors.AppBarPopupMenuTextColor)))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomBar = BottomNavigationBar(
      items: _navigationIconViews
          .map((NavigationIconView view) => view.item)
          .toList(),
      currentIndex: _currentIndex,
      // 设置选中和未选中主题
      selectedIconTheme: IconThemeData(color: Color(AppColors.TabIconActive)),
      selectedItemColor: Color(AppColors.TabIconActive),
      unselectedIconTheme: IconThemeData(color: Color(AppColors.TabIconNormal)),
      unselectedItemColor: Color(AppColors.TabIconNormal),
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        print("点击底部bar:$index");
        setState(() {
          _currentIndex = index;
        });
      },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text("微信"),
        elevation: 0, // 设置不需要阴影
        actions: [
          IconButton(
              onPressed: () {
                print("搜索按钮");
              },
              icon: Icon(Icons.search)),
          PopupMenuButton(
            itemBuilder: (context) {
              return <PopupMenuEntry<ActionItems>>[
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe761, "发起群聊"),
                  value: ActionItems.GROUP_CHAT,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe761, "添加朋友"),
                  value: ActionItems.ADD_FRIEND,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe761, "扫一扫"),
                  value: ActionItems.QS_SCAN,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe761, "收付款"),
                  value: ActionItems.PAYMENT,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe761, "帮助与反馈"),
                  value: ActionItems.HELP,
                ),
              ];
            },
            icon: Icon(Icons.add),
            onSelected: (ActionItems item) {
              print(item);
            },
          )
        ],
      ),
      body: Container(
        color: Colors.white,
      ),
      bottomNavigationBar: bottomBar,
    );
  }
}
